<template>
    <base-modal
        :title="title"
        cancelText="取消"
        okText="保存"
        :width="900"
        v-on="$listeners"
        @ok="handleOk"
        :confirmLoading="loading.isSaving"
    >
        <a-form-model
            ref="form"
            :model="formInfo"
            :rules="rules"
            :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
            :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
        >
            <a-row :gutter="24">
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="序号" prop="sortNum">
                        <BaseInput
                            :disabled="initData.pageType == 'look'"
                            placeholder="请输入"
                            v-model.trim="formInfo.sortNum"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="分部分项" prop="subItem">
                        <BaseInput
                            :disabled="initData.pageType == 'look'"
                            placeholder="请输入"
                            v-model.trim="formInfo.subItem"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="特征描述" prop="featureDescription">
                        <BaseTextarea
                            :disabled="initData.pageType == 'look'"
                            
                            placeholder="请输入"
                            v-model.trim="formInfo.featureDescription"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="工作内容" prop="workContent">
                        <BaseTextarea
                            :disabled="initData.pageType == 'look'"
                            
                            placeholder="请输入"
                            v-model.trim="formInfo.workContent"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="单位" prop="unit">
                        <BaseInput
                            :disabled="initData.pageType == 'look'"
                            placeholder="请输入"
                            v-model.trim="formInfo.unit"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="分包价格(元)" prop="subcontractPrice">
                        <BaseInputNumber
                            :disabled="initData.pageType == 'look'"
                            placeholder="请输入"
                            v-model.trim="formInfo.subcontractPrice"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="计算规则" prop="calcRule">
                        <BaseTextarea
                            :disabled="initData.pageType == 'look'"
                            
                            placeholder="请输入"
                            v-model.trim="formInfo.calcRule"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="分包模式" prop="subcontractMode">
                        <BaseTextarea
                            :disabled="initData.pageType == 'look'"
                            
                            placeholder="请输入"
                            v-model.trim="formInfo.subcontractMode"
                        />
                    </a-form-model-item>
                </a-col>
                <a-col :md="12" :sm="12">
                    <a-form-model-item label="备注">
                        <BaseTextarea
                            :disabled="initData.pageType == 'look'"
                            
                            placeholder="请输入"
                            v-model.trim="formInfo.remark"
                        />
                    </a-form-model-item>
                </a-col>
            </a-row>
        </a-form-model>
        <template slot="footer" style="text-align: right" class="page-btn-right-top">
            <div v-if="initData.pageType != 'look'">
                <a-button
                    :disabled="initData.pageType == 'look'"
                    :loading="loading.isSaving"
                    type="primary"
                    class="mr20"
                    @click="handleOk"
                    >保存</a-button
                >
                <a-button @click="handleClose">取消</a-button>
            </div>
            <div v-else class="page-btn-right-top">
                <a-button @click="handleClose">关闭</a-button>
            </div>
        </template>
    </base-modal>
</template>

<script>
import { addManagement, editManagement, checkManagement } from '@/api/businessAffairs/subcontractCostPrice.js'
import BaseModal from '@/components/BaseModal'
export default {
    name:'a'+Date.now(),
    components: {
        BaseModal,
    },
    props: {
        initData: {
            type: Object,
            default() {
                return {}
            },
        },
    },
    data() {
        return {
            loading: {
                isSaving: false,
            },
            title: '',
            formInfo: {},
            rules: {
                sortNum: [{ required: true, message: '请输入', trigger: 'change' }],
                subItem: [{ required: true, message: '请输入', trigger: 'change' }],
                featureDescription: [{ required: true, message: '请输入', trigger: 'change' }],
                workContent: [{ required: true, message: '请输入', trigger: 'change' }],
                unit: [{ required: true, message: '请输入', trigger: 'change' }],
                subcontractPrice: [{ required: true, message: '请输入', trigger: 'change' }],
                calcRule: [{ required: true, message: '请输入', trigger: 'change' }],
                subcontractMode: [{ required: true, message: '请输入', trigger: 'change' }],
            },
        }
    },
    created() {
        this.getDetail()
    },
    methods: {
        handleClose() {
            this.$emit('cancel')
        },
        validateForm() {
            let flag = false
            this.$refs.form.validate((valid) => {
                flag = valid
            })
            return flag
        },
        async getDetail() {
            if (this.initData.pageType == 'edit') {
                this.title = '资料项管理编辑'
            } else if (this.initData.pageType == 'add') {
                this.title = '资料项管理添加'
            } else {
                this.title = '资料项管理查看'
            }
            const { id, cateId } = this.initData
            if (!id) return
            const params = {
                id,
                cateId,
            }
            const [err, res] = await checkManagement(params)
            if (!err) {
                this.formInfo = {
                    ...this.formInfo,
                    ...res.data,
                }
            }
        },
        async handleOk() {
            if (!this.validateForm()) return
            if (this.loading.isSaving) return
            this.loading.isSaving = true
            // const { id, name, enabled, guidId } = this.formInfo
            const params = {
                ...this.formInfo,
                cateId: this.initData.cateId,
            }
            console.log(params)
            const { pageType } = this.initData
            const fn = pageType === 'edit' ? editManagement : addManagement
            const [err, res] = await fn(params)
            if (!err) {
                this.$message.success('成功')
                this.$emit('confirm')
            }
            this.loading.isSaving = false
        },
    },
}
</script>

<style lang="less" scoped>
form .ant-mentions,
form textarea.ant-input {
    height: auto !important;
    width: 100%;
}
</style>